package gold.digger;

import java.util.HashSet;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC128 {

    public int longestConsecutive(int[] arr) {
        if (null == arr || arr.length <= 0) {
            return 0;
        }

        HashSet<Integer> dict = new HashSet<>();
        for (int num : arr) {
            dict.add(num);
        }

        int less, more = 0, maxLength = 0;
        for (int num : arr) {
            less = num - 1;
            more = num + 1;
            dict.remove(num);
            while (dict.contains(less)) {
                dict.remove(less--);
            }
            while (dict.contains(more)) {
                dict.remove(more++);
            }

            if (more - less - 1 > maxLength) {
                maxLength = more - less - 1;
            }
        }

        return maxLength;
    }


    public static void main(String[] args) {
        LC128 an = new LC128();
        int[] arr = {0, -1};
        System.out.println(an.longestConsecutive(arr));
    }
}
